<template>
  <div>
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="姓名：">
        <el-input v-model.trim="query.name"></el-input>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select v-model="query.status" clearable filterable style="width: 85px">
          <el-option v-for="item in statusOptions" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button icon="el-icon-circle-plus-outline" type="primary" @click="openAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <edit :title="edit.title" :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose"></edit>

    <template>
      <el-table :data="list" style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
        <el-table-column align="center" prop="name" label="分类名称"></el-table-column>
        <el-table-column align="center" prop="sort" label="排序" sortable></el-table-column>
        <el-table-column align="center" prop="remark" label="备注"></el-table-column>
        <el-table-column align="center" prop="status" label="状态" width="160">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">
              {{scope.row.status ? '正常' : '禁用'}}
            </el-tag>
            <!-- <el-tag :type="{{statusFilter(scope.row.status)}}"></el-tag> -->
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.current"
          :page-sizes="[10, 20, 30]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
        ></el-pagination>
      </div>
    </template>
  </div>
</template>

<script>
import api from '@/api/category';
import Edit from './edit';
//用于下拉框
const statusOptions = [
  {code: 0,name:'禁用'},
  {code: 1,name:'正常'}
];
export default {
  components: {Edit},
  data() {
    return {
      list: [],
      page: {
        total: 0,
        current: 1,
        size: 20
      },
      edit:{
        title:"",
        visible:false,
         formData: {
                  id: null,
                  name: "",
                  sort: null,
                  remark: "",
                },
      },
      query: {},
      statusOptions
    };
  },
  created() {
    this.fetchData();
  },

  filters: {
    statusFilter(status) {
      // 样式
      const statusMap = { 0: 'danger', 1: 'success' };
      // status等于0返回danger, 1返回success
      return statusMap[status];
    }
  },
  methods: {
    openAdd(){
      this.edit.visible = true
      this.edit.title = '新增'
    },
    remoteClose(){
      this.edit.formData = {}
      this.edit.visible = false
      this.fetchData()
    },
    reload(){
      this.query = {}
      this.fetchData()
    },
    queryData(){
      this.page.current = 1
      this.fetchData()
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.fetchData();
    },
    handleEdit(id) {
      console.log('编辑', id);
      api.getById(id).then(response =>{
        console.log(response.data)
        if(response.code === 20000){
          this.edit.formData = response.data
          this.edit.title = '编辑'
          this.edit.visible = true
        }
      })
    },
    handleDelete(id) {
      console.log('删除', id);
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                api.deleteById(id).then(response =>{
                  console.log(response)
                  this.$message({
                    type: response.code===20000 ? 'success' : 'error',
                    message: response.message
                  });
                  this.fetchData()
                })

              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: '已取消删除'
                });
              });
    },

    fetchData() {
      api.getList(this.query, this.page.current, this.page.size).then((response) => {
        console.log(this.page.size);
        this.list = response.data.records;
        this.page.total = response.data.total;
      });
    }
  }
};
</script>

<style></style>
